package com.frxuexi.park2302

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.mvi.BaseViewActivity
import com.frxuexi.park2302.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask

class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {
    override val binding: ActivitySplashBinding by lazy { ActivitySplashBinding.inflate(layoutInflater) }

    var num=5
    var timer:Timer?=null
    override fun initData() {
    }

    override fun initView() {
        val inTime = SPUtils.getInstance().getLong("inTime", 0L)
        val time=(System.currentTimeMillis()-inTime)/1000/60/24
        if(time<=7){
            ARouter.getInstance().build("/app/LoginActivity").navigation()
            finish()
        }
        lifecycleScope.launch (Dispatchers.IO){
            timer= Timer()
            timer!!.schedule(timerTask {
                lifecycleScope.launch (Dispatchers.Main){
                    binding.splashTime.setText("${num--}s")
                    if(num==0){
                        start()
                    }
                }
            },0,1000)
        }

        binding.splashSkip.setOnClickListener {
            start()
        }
    }

    fun start(){
        SPUtils.getInstance().put("inTime",System.currentTimeMillis())
        timer!!.cancel()
        ARouter.getInstance().build("/app/LoginActivity").navigation()
    }

    override fun onDestroy() {
        super.onDestroy()
        if(timer!=null){
            timer?.cancel()
            timer=null
        }
    }
}